package com.weilong.lw.mytest;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

import static com.weilong.lw.mytest.NetUtil.ALBUM_PATH;
import static com.weilong.lw.mytest.NetUtil.IMG;
import static com.weilong.lw.mytest.NetUtil.getInstance;

public class DHActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img;
    private EditText et_path;
    private Button btn_shezhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dh);
        initView();
        setView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3 * 1000);
                    handler.sendEmptyMessage(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setView() {
        //实例化SharedPreferences对象（第一步）
        SharedPreferences mySharedPreferences = getSharedPreferences("test",
                Activity.MODE_PRIVATE);
        // 使用getString方法获得value，注意第2个参数是value的默认值
        String path = mySharedPreferences.getString("path", "");
        Log.i("lw", "path====" + path);
        NetUtil util=getInstance();
        if (path.length() == 0) {

            handler.sendEmptyMessage(2);
        } else {
            //对保存图片的名称和内存中的key值进行设置，防止冲突，从地址中截取一段字符串
            String str=path.substring(path.length()-12,path.length()-4);
            IMG=str;
                Log.i("lw", "cache===="+util.getBitmapFromMemCache(IMG));
            if (util.getBitmapFromMemCache(IMG)!=null){
                Log.i("lw", "cache");
                Bitmap dh_img = util.getBitmapFromMemCache(IMG);
                img.setImageBitmap(dh_img);
            }else {
                File dirFile = new File(ALBUM_PATH + IMG);
                if (dirFile.exists()) {
                    Log.i("lw", "exists");
                    handler.sendEmptyMessage(4);
                } else {
                    Log.i("lw", "!!!!exists");

                    util.getDH_IMG1(handler, path);
//            util.getDH_IMG(handler, path);
                }
            }
        }


    }

    private void initView() {
        img = (ImageView) findViewById(R.id.dh_img);
        et_path = (EditText) findViewById(R.id.et_path);

        btn_shezhi = (Button) findViewById(R.id.btn_shezhi);
        btn_shezhi.setOnClickListener(this);

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
//                Bitmap bitmap = (Bitmap) msg.obj;
//                img.setImageBitmap(bitmap);
                boolean b = (boolean) msg.obj;
                Log.i("lw", "b=====" + b);

//                    FileInputStream stream = new FileInputStream(NetUtil.ALBUM_PATH+NetUtil.IMG);
                if (b) {
                    Bitmap bitmap = BitmapFactory.decodeFile(ALBUM_PATH + IMG);
                    img.setImageBitmap(bitmap);
                }else {
                    Toast.makeText(DHActivity.this,"内存卡不存在或者",Toast.LENGTH_SHORT).show();
                }
            } else if (msg.what == 2) {
                img.setImageResource(R.drawable.img_dh2);

            } else if (msg.what == 3) {
                img.setVisibility(View.GONE);
                et_path.setVisibility(View.VISIBLE);
                btn_shezhi.setVisibility(View.VISIBLE);
            }else if (msg.what==4){
                try {
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        Bitmap bitmap = BitmapFactory.decodeFile(ALBUM_PATH + IMG);
                        img.setImageBitmap(bitmap);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_shezhi:
                //实例化SharedPreferences对象（第一步）
                SharedPreferences mySharedPreferences = getSharedPreferences("test",
                        Activity.MODE_PRIVATE);

                //实例化SharedPreferences.Editor对象（第二步）
                SharedPreferences.Editor editor = mySharedPreferences.edit();
                //用putString的方法保存数据
                editor.putString("path", et_path.getText().toString().trim());
                //提交当前数据
                editor.commit();
                break;
        }
    }


}
